package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.adm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.EmployeeCertificateMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.EmployeeCertificate;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeCertificateVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.IamFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.IamEmployeeCertificateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Package com.cqhilink.iot.integratedapplicationmanagement.services.innerservice.adm.impl
 * @Description: ${TODO}(人员证书服务实现)
 * @author: lutianbo
 * @date: 2016/12/15,0015
 */

public class IamEmployeeCertificateServiceImpl implements IamEmployeeCertificateService
{
    @Autowired
    private EmployeeCertificateMapper employeeCertificateMapper;
    @Autowired
    private IamFileService iamFileService;

    /**
     * 查询人员证书列表
     * @param bootstrapSearchBean 搜索参数
     * @return
     */
    @Override
    public JSONObject list(BootstrapSearchBean bootstrapSearchBean) {
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        bootstrapSearchBean.setLimit( bootstrapSearchBean.getLimit() == null ? Integer.valueOf(10) : bootstrapSearchBean.getLimit() );
        bootstrapSearchBean.setOffset( bootstrapSearchBean.getOffset() == null ? Integer.valueOf(0) : bootstrapSearchBean.getOffset() );
        // 总条数
        jsonObject.put("total",employeeCertificateMapper.queryCount(bootstrapSearchBean));
        List<EmployeeCertificateVo> trainList = employeeCertificateMapper.queryList(bootstrapSearchBean);
        jsonObject.put("rows", JSON.toJSON(trainList));
        return jsonObject;
    }

    /**
     * 删除人员证书
     * @param employeeCertificateId 人员证书id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String[] employeeCertificateId) {
        if(employeeCertificateId.length>0){
            return employeeCertificateMapper.deleteByPrimaryKey(employeeCertificateId);
        }
        return 0;
    }

    /**
     * 新增人员证书
     * @param record
     * @param uploadFiles 需要上传的证书文件
     * @param deletedFiles 需要删除的证书文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(EmployeeCertificate record,String uploadFiles,String deletedFiles) {
        // 数据保存成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return resultCount;
        }
        resultCount +=  employeeCertificateMapper.insertSelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getEmployeeCertificateId());

        return  resultCount;
    }

    /**
     * 根据id查询人员证书
     * @param employeeCertificateId 人员证书id
     * @return
     */
    @Override
    public EmployeeCertificate selectByPrimaryKey(String employeeCertificateId) {
        if(!StringUtils.isEmpty(employeeCertificateId)){
            return  employeeCertificateMapper.selectByPrimaryKey(employeeCertificateId);
        }
        return null;
    }

    /**
     * 更新人员证书
     * @param record  人员证书实体
     * @param uploadFiles 需要上传的证书文件
     * @param deletedFiles 需要删除的证书文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(EmployeeCertificate record,String uploadFiles,String deletedFiles) {
        // 数据保存成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return  resultCount;
        }
        resultCount += employeeCertificateMapper.updateByPrimaryKeySelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getEmployeeCertificateId());
        return  resultCount;
    }

    /**
     * 验证是否已经存在
     * @param empId 用户id
     * @param cerId 证书id
     * @return
     */
    @Override
    public boolean isExistById(String cerId ,String empId) {
        Map<String,Object> map = new HashMap<>();
        map.put("empId",empId);
        map.put("cerId",cerId);

        return employeeCertificateMapper.isExistById(map) > 0 ? true : false;
    }
}
